﻿//--------------------------------------------------------------------
// 动态报表页脚控件 v1.00
//
// 开发人： 朱明智
// 创建时间： 2019-08-22
//
// 修改人：
// 修改时间：
//--------------------------------------------------------------------

using System.Collections.Generic;

namespace Dynamic.Controller
{
    /// <summary>
    /// 页脚类
    /// </summary>
    public class PageFooter : ReportController
    {
        private List<Row> _rectangles = new List<Row>();

        /// <summary>
        /// 容器中控件高度综合
        /// </summary>
        private float _TextBoxHeight
        {
            get
            {
                float hight = 0f;

                foreach (var item in _rectangles) hight += item.Height;

                return hight;
            }
        }

        public float Height
        {
            get { return base.height; }
            private set { base.height = value; }
        }

        public PageFooter()
        {
            template = TemplateHelper.PageFooterTemp;
        }

        /// <summary>
        /// 添加矩形容器
        /// </summary>
        public void AddRectangle(Row rectangle)
        {
            _rectangles.Add(rectangle);
            rectangle.Parent = this;
        }

        // 计算修正位置Caption & Rectangles
        private void CalculationPosition()
        {
            // 修正页眉高度
            if (Height < _TextBoxHeight) Height = _TextBoxHeight;
            float top = 0f;
            for (int i = 0; i < _rectangles.Count; i++)
            {
                var rectangle = _rectangles[i];
                rectangle.Top = top;
                top += rectangle.Height;
            }
        }

        public override string ToString()
        {
            CalculationPosition();

            var str = base.ToString();

            for (var index = 0; index < _rectangles.Count; index++)
            {
                var rectangle = _rectangles[index];
                // 最后一个
                if (index == _rectangles.Count - 1)
                    str = str.Replace("@Rectangle", rectangle.GetTempStr());
                else
                    str = str.Insert(str.IndexOf("@Rectangle"), rectangle.GetTempStr());
            }

            if (_rectangles.Count == 0)
                str = str.Replace("<ReportItems></ReportItems>", "");
            return str;
        }
       
    }
}